import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_base_widget/base/base.dart';

class ScreamBuilderScreen extends BasePage {
  @override
  Widget rebuild(BuildContext context) {
    return WillPopScope(
      onWillPop: (){
        print("pop");
      },
      child: StreamBuilder(
        stream: NumberCreator().stream,
        builder: (context, snapshot){
          if(snapshot.connectionState == ConnectionState.waiting){
            return Text("No data yet");
          } else if(snapshot.connectionState == ConnectionState.done) {
            return Text("done");
          } else if(snapshot.hasError) {
            return Text("Error" + snapshot.error);
          } else if(snapshot.hasData) {
            return Text(snapshot.data.toString());
          }
        },
      ),
    );
  }
}

class NumberCreator {
  
  var _count = 1;
  final _controller = StreamController<int>();

  Stream<int> get stream => _controller.stream;
  Timer _timer;

  NumberCreator(){
    _timer = Timer.periodic(Duration(seconds: 1), (t){
      if(_count > 5) {
        _timer.cancel();
        _controller.close();
      } else if(_count == 3){
        _controller.addError("_count can not is 3");
      } else{
        _controller.sink.add(_count);
      }
      _count++;
    });
  }

}